#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" This module defines the URL namespace for TMPO.

"""
from functools import partial
from os.path import abspath, dirname, join
from tack.lib import no_cache_app, compose

from tack.lib.urlns import *
from tack_default import api as default_api
from tack_tmpo.api import drivers, driver_schedule
from tack_tmpo.api import du
from tack_tmpo.api import vehicles
from tack_tmpo.api import vehicletype
from tack_tmpo.api import vehicleinfo, maintinfo
from tack_tmpo.api import vhistory
from tack_tmpo.api import vu
##from tack_tmpo.api import timeline
from tack_tmpo.api import vu_type
from tack_tmpo.api import du_type
from tack_tmpo.api import resources
##from tack_tmpo.api import schedule_put, schedule_put_public, schedule_post, schedule_get, schedule_delete
from tack_tmpo.api import util, commitment_put, commitment_get, commitment_post, commitment_delete
from tack_tmpo.api import commitment_leg_put, commitment_leg_post, commitment_leg_delete
##from tack_tmpo.api import schedule_post, schedule_get, schedule_delete
from tack_tmpo.api import site_config
from tack_tmpo.api import reports
from tack_tmpo.api import report_results 
from tack_tmpo.api import notify


here = abspath(dirname(__file__))
static_dir = join(here, 'static')
htdoc_file = partial(join, static_dir, 'htdoc')
local404 = template_app(htdoc_file('error_404.html'), status=status_codes.not_found)


def getdsn(env):
    try:
        return env['tack.siteconfig']['tmpo_dsn']
    except (KeyError, ):
        pass

# our URL DSL
DB = partial(confdbcon, dsn_lookup=getdsn)
FORM = formreader
FILES = partial(fileserver, handle404=local404)
NC = no_cache_app
COOKIE = cookie_monster
COOKIEAUTH = default_api.cookie_auth_app
SETROLES = default_api.set_login_roles_app
NEEDROLES = default_api.require_roles_app
ADMINROLES = ['TMPO Admin']
DRIVERROLES = ['TMPO Driver']
DISPATCHERROLES = ['TMPO Dispatcher']
TESTFAILROLE = ['TMPO Fail Role']

def GUARD(app, roles):
    return DB(COOKIE(COOKIEAUTH(SETROLES(NEEDROLES(app, roles)))))
    
def FGUARD(app, roles):
    return FORM(DB(COOKIE(COOKIEAUTH(SETROLES(NEEDROLES(app, roles))))))
    
def NCGUARD(app, roles):
    return NC(DB(COOKIE(COOKIEAUTH(SETROLES(NEEDROLES(app, roles))))))
    
## TODO: Reduce to a single definition
requested, conflicted, resourced, canceled = range(1,5)

## TODO: Reduce unnecessary url defs. 
urls = [
    ('/api/ztest/','GET', GUARD(echo_app(), ADMINROLES)),
    ('/api/tmpo/du_type/','GET', DB(du_type.get_all_types)),
    ('/api/tmpo/du_type/', 'PUT', FGUARD(du_type.add_type, DISPATCHERROLES)),
    ('/api/tmpo/du_type/(?P<du_type_id>\d+)/', 'GET', DB(du_type.get_type_data)),
    ('/api/tmpo/du_type/(?P<du_type_id>\d+)/', 'POST', FGUARD(du_type.update_type, DISPATCHERROLES)),
    ('/api/tmpo/du_type/(?P<du_type_id>\d+)/', 'DELETE', GUARD(du_type.delete_type, DISPATCHERROLES)),

    ('/api/tmpo/vu_type/','GET', DB(vu_type.get_all_types)),
    ('/api/tmpo/vu_type/', 'PUT', FGUARD(vu_type.add_type, DISPATCHERROLES)),
    ('/api/tmpo/vu_type/(?P<vu_type_id>\d+)/', 'GET', DB(vu_type.get_type_data)),
    ('/api/tmpo/vu_type/(?P<vu_type_id>\d+)/', 'POST', FGUARD(vu_type.update_type, DISPATCHERROLES)),
    ('/api/tmpo/vu_type/(?P<vu_type_id>\d+)/', 'DELETE', GUARD(vu_type.delete_type, DISPATCHERROLES)),

    ('/api/tmpo/vehicletype/','GET', GUARD(vehicletype.get_all_types, DISPATCHERROLES)),
    ('/api/tmpo/vehicletype/', 'PUT', FGUARD(vehicletype.add_type, DISPATCHERROLES)),
    ('/api/tmpo/vehicletype/(?P<type_id>\d+)/', 'GET', GUARD(vehicletype.get_type_data, DISPATCHERROLES)),
    ('/api/tmpo/vehicletype/(?P<type_id>\d+)/', 'POST', FGUARD(vehicletype.update_type, DISPATCHERROLES)),
    ('/api/tmpo/vehicletype/(?P<type_id>\d+)/', 'DELETE', GUARD(vehicletype.delete_type, DISPATCHERROLES)),

##    ('/api/tmpo/timeline/', 'GET', GUARD(timeline.timeline, DISPATCHERROLES)),

    ('/api/tmpo/drivers/schedule/(?P<date>\d{4}[-]\d{2}[-]\d{2})/', 'GET', GUARD(driver_schedule.get_based_on_date_range, DISPATCHERROLES)),

    ('/api/tmpo/drivers/', 'GET', GUARD(drivers.get_all_drivers, DISPATCHERROLES)),
    ('/api/tmpo/drivers/', 'PUT', FGUARD(drivers.add_driver, DISPATCHERROLES)), 
    ('/api/tmpo/drivers/(?P<driver_id>\d+)/', 'GET', GUARD(drivers.get_driver_data, DISPATCHERROLES)),
    ('/api/tmpo/drivers/(?P<start>.*)/(?P<finish>.*)/', 'GET', GUARD(drivers.get_available_drivers, DISPATCHERROLES)),
    ('/api/tmpo/drivers/(?P<driver_id>\d+)/', 'POST', FGUARD(drivers.update_driver, DISPATCHERROLES)),
    ('/api/tmpo/drivers/(?P<driver_id>.*)/', 'DELETE', GUARD(drivers.delete_driver, DISPATCHERROLES)),

    ('/api/tmpo/vehicles/history/action/(?P<vha_id>\d+)/', 'POST', FGUARD(vhistory.update_action, DISPATCHERROLES)),
    ('/api/tmpo/vehicles/history/action/(?P<vha_id>\d+)/', 'DELETE', GUARD(vhistory.delete_vha, DISPATCHERROLES)),
    ('/api/tmpo/vehicles/history/action/', 'PUT', FGUARD(vhistory.add_vha, DISPATCHERROLES)),
    ('/api/tmpo/vehicles/history/destination/(?P<vhd_id>\d+)/', 'POST', FGUARD(vhistory.update_destination, DISPATCHERROLES)),
    ('/api/tmpo/vehicles/history/destination/(?P<vhd_id>\d+)/', 'DELETE', GUARD(vhistory.delete_vhd, DISPATCHERROLES)),
    ('/api/tmpo/vehicles/history/destination/', 'PUT', FGUARD(vhistory.add_vhd, DISPATCHERROLES)),
    ('/api/tmpo/vehicles/history/', 'PUT', FGUARD(vhistory.add_vh, DISPATCHERROLES)),
    ('/api/tmpo/vehicles/history/(?P<vh_id>\d+)/', 'POST', FGUARD(vhistory.update_vh, DISPATCHERROLES)),
    ('/api/tmpo/vehicles/history/(?P<vehicle_id>\d+)/(?P<start>.*)/(?P<finish>.*)/', 'GET', GUARD(vhistory.get_vhistory, DISPATCHERROLES)),
    ('/api/tmpo/vehicles/history/(?P<vehicle_id>\d+)/(?P<date>.*)/', 'GET', GUARD(vhistory.get_vhistory_keyed, DISPATCHERROLES)),
    
    ('/api/tmpo/vehicles/maintinfo/', 'GET', GUARD(maintinfo.get_maint_info, DISPATCHERROLES)),
    ('/api/tmpo/vehicles/maintinfo/', 'PUT', FGUARD(maintinfo.add_maint_event, DISPATCHERROLES)),
    ('/api/tmpo/vehicles/maintinfo/(?P<vu_id>\d+)/', 'POST', FGUARD(maintinfo.update_maint_event, DISPATCHERROLES)),
    ('/api/tmpo/vehicles/maintinfo/(?P<vu_id>\d+)/', 'DELETE', GUARD(maintinfo.delete_maint_event, DISPATCHERROLES)),
    
    ('/api/tmpo/vehicles/', 'GET', GUARD(vehicles.get_all_vehicles, DISPATCHERROLES)),
    ('/api/tmpo/vehicles/', 'PUT', FGUARD(vehicles.add_vehicle, DISPATCHERROLES)), 
    ('/api/tmpo/vehicles/(?P<vehicle_id>\d+)/', 'GET', GUARD(vehicles.get_vehicle_data, DISPATCHERROLES)),
    ('/api/tmpo/vehicles/(?P<vehicle_id>\d+)/', 'POST', FGUARD(vehicles.update_vehicle, DISPATCHERROLES)),
    ('/api/tmpo/vehicles/(?P<start>.*)/(?P<finish>.*)/', 'GET', GUARD(vehicles.get_available_vehicles, DISPATCHERROLES)),
    ('/api/tmpo/vehicles/(?P<vehicle_id>\d+)/', 'DELETE', GUARD(vehicles.delete_vehicle, DISPATCHERROLES)),

    ('/api/tmpo/vehicleinfo/', 'GET', GUARD(vehicleinfo.get_vehicle_state, DISPATCHERROLES)),

    ('/api/tmpo/resources/', 'GET', GUARD(resources.get_all_resources, DISPATCHERROLES)),
    ('/api/tmpo/resources/(?P<start>.*)/(?P<finish>.*)/(?P<record_id>\d+)/', 'GET', GUARD(resources.get_available_resources, DISPATCHERROLES)),
    ('/api/tmpo/resources/(?P<start>.*)/(?P<finish>.*)/', 'GET', GUARD(resources.get_available_resources, DISPATCHERROLES)),
     
    ('/api/tmpo/du/', 'GET', GUARD(du.get_all_events, DISPATCHERROLES)),
    ('/api/tmpo/du/', 'PUT', FGUARD(du.add_event, DISPATCHERROLES)), 
    ('/api/tmpo/du/(?P<du_id>\d+)/', 'GET', GUARD(du.get_event_data, DISPATCHERROLES)),
    ('/api/tmpo/du/(?P<du_id>\d+)/', 'POST', FGUARD(du.update_event, DISPATCHERROLES)),
    ('/api/tmpo/du/(?P<du_id>\d+)/', 'DELETE', GUARD(du.delete_event, DISPATCHERROLES)),
    ('/api/tmpo/du/driver/(?P<driver_id>\d+)/', 'GET', GUARD(du.get_event_data_on_driver, DISPATCHERROLES)),
    ('/api/tmpo/du/(?P<start>.*)/(?P<finish>.*)/', 'GET', GUARD(du.get_event_data_based_on_date_range, DISPATCHERROLES)),
    ('/api/tmpo/du/driver/(?P<driver_id>\d+)/(?P<start>.*)/(?P<finish>.*)/', 'GET', GUARD(du.get_driver_event_data_based_on_date_range, DISPATCHERROLES)),

    ('/api/tmpo/vu/', 'GET', GUARD(vu.get_all_events, DISPATCHERROLES)),
    ('/api/tmpo/vu/current/', 'GET', GUARD(vu.get_current_vehicles, DISPATCHERROLES)),
    ('/api/tmpo/vu/', 'PUT', FGUARD(vu.add_event, DISPATCHERROLES)), 
    ('/api/tmpo/vu/(?P<vu_id>\d+)/', 'GET', GUARD(vu.get_event_data, DISPATCHERROLES)),
    ('/api/tmpo/vu/(?P<vu_id>\d+)/', 'POST', FGUARD(vu.update_event, DISPATCHERROLES)),
    ('/api/tmpo/vu/(?P<vu_id>\d+)/', 'DELETE', GUARD(vu.delete_event, DISPATCHERROLES)),
    ('/api/tmpo/vu/vehicle/(?P<vehicle_id>\d+)/', 'GET', GUARD(vu.get_event_data_on_vehicle, DISPATCHERROLES)),
    ('/api/tmpo/vu/(?P<start>.*)/(?P<finish>.*)/', 'GET', GUARD(vu.get_event_data_based_on_date_range, DISPATCHERROLES)),

    ('/api/tmpo/commitment/(?P<date>\d{4}[-]\d{2}[-]\d{2})/', 'GET', GUARD(commitment_get.get_by_date, DISPATCHERROLES)),
    ('/api/tmpo/commitment/requested/', 'GET', GUARD(commitment_get.get_requested, DISPATCHERROLES)),
    ('/api/tmpo/commitment/requested/', 'PUT', FORM(DB(commitment_put.insert_requested))),
    ('/api/tmpo/commitment/conflicted/', 'GET', GUARD(commitment_get.get_conflicted, DISPATCHERROLES)),
    ('/api/tmpo/commitment/resourced/', 'PUT', FGUARD(commitment_put.insert_resourced, DISPATCHERROLES)),

    ('/api/tmpo/commitment/poc_details/(?P<commitment_id>\d+)/', 'POST', FGUARD(commitment_post.update_poc, DISPATCHERROLES)),
    
    ('/api/tmpo/commitment/(?P<commitment_id>\d+)/', 'GET', GUARD(commitment_get.get_by_commitment_id, DISPATCHERROLES)),
    ('/api/tmpo/commitment/(?P<commitment_id>\d+)/', 'DELETE', GUARD(commitment_delete.delete_by_commitment_id, DISPATCHERROLES)),
    ('/api/tmpo/commitment/requested/(?P<commitment_id>\d+)/', 'POST', FGUARD(commitment_post.update_requested, DISPATCHERROLES)),
    ('/api/tmpo/commitment/conflicted/(?P<commitment_id>\d+)/', 'POST', FGUARD(commitment_post.update_conflicted, DISPATCHERROLES)),
    ('/api/tmpo/commitment/resourced/(?P<commitment_id>\d+)/', 'POST', FGUARD(commitment_post.update_resourced, DISPATCHERROLES)),
    ('/api/tmpo/commitment/leg/(?P<commitment_id>\d+)/', 'PUT', FGUARD(commitment_leg_put.insert_leg, DISPATCHERROLES)),
    ('/api/tmpo/commitment/requested/leg/(?P<commitment_id>\d+)/', 'PUT', FORM(DB(commitment_leg_put.insert_requested_leg))),
    ('/api/tmpo/commitment/leg/(?P<commitment_id>\d+)/', 'DELETE', GUARD(commitment_leg_delete.delete_legs, DISPATCHERROLES)),
    ('/api/tmpo/commitment/leg/(?P<commitment_leg_id>\d+)/', 'POST', FGUARD(commitment_leg_post.update_leg, DISPATCHERROLES)),
    ('/api/tmpo/commitment/notify/(?P<commitment_id>\d+)/', 'GET', DB(notify.notify_setup)),  

    ('/api/tmpo/reports/', 'PUT', FGUARD(reports.add_report, DISPATCHERROLES)),
    ('/api/tmpo/reports/', 'GET', GUARD(reports.get_all, DISPATCHERROLES)),
    ('/api/tmpo/reports/(?P<report_id>\d+)/', 'POST', FGUARD(reports.update_report, DISPATCHERROLES)),
    ('/api/tmpo/reports/(?P<report_id>\d+)/', 'DELETE', GUARD(reports.delete_report, DISPATCHERROLES)),
    
    ('/api/tmpo/report_results/(?P<report_id>\d+)/', 'POST', FGUARD(report_results.get_results, DISPATCHERROLES)),

    ('/api/tmpo/config_public/', 'GET', site_config.get_public),
    ('/api/tmpo/config_public/', 'POST', FGUARD(site_config.post_public, DISPATCHERROLES)),
    
    ('/api/tmpo/options/email/', 'GET', DB(site_config.get_email_data)),
    ('/api/tmpo/options/email/', 'POST', FGUARD(site_config.update_email_data, DISPATCHERROLES)),
    
    ('/api/tmpo/role_check/', 'GET', GUARD(default_api.k_checker, DISPATCHERROLES)), 

    ('/tmpo/(?P<fname>.*)',  'GET',  FILES(static_dir)),
]
